<template>
  <a-modal
    title="料品"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-show="false"
        >
          <a-input
            :disabled="showable"
            v-decorator="['id']"/>
        </a-form-item>
        <a-form-item
          label="料品编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            placeholder="请输入"
            v-decorator="['number', {rules: [{required: true, message: '填写料品编码'}]}]" />
        </a-form-item>
        <a-form-item
          label="料品名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入"
            v-decorator="['name', {rules: [{required: true, message: '填写料品名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="规格型号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入"
            v-decorator="['specifications']" />
        </a-form-item>
        <a-form-item
          label="父级产品"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-tree-select
            showSearch
            placeholder="选择父级产品"
            allowClear
            :treeData="treeData"
            treeDefaultExpandAll
            treeNodeFilterProp="title"
            v-decorator="['parentNumber', {rules: [{required: true, message: '选择父级产品'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="料品属性"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入"
            v-decorator="['prope']" />
        </a-form-item>
        <a-form-item
          label="配比系数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入"
            v-decorator="['matching']" />
        </a-form-item>
        <a-form-item
          label="计量单位"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入"
            v-decorator="['unit']" />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入"
            v-decorator="['remark']" />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer" >
      <div
        v-show="editable || addable"
      >
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>

</template>

<script>
import pick from 'lodash.pick'
import { getObj, addObj, putObj, getTree } from '@api/boms/productBom'
import { treeDataTranslate } from '@/utils/util'

export default {
  name: 'ProductAddOrUpdate',
  components: {},
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      editable: false,
      addable: false,
      showable: false,
      treeData: [],
      rangeDate: [],
      type: 'add',
      number: ''
    }
  },
  methods: {
    // 获取用户信息
    edit (num, type, number) {
      this.visible = true
      this.number = number
      if (type && type === 'add') {
        this.addable = true
        this.type = type
      }
      if (type === 'edit') {
        this.editable = true
        this.showable = true
        this.type = type
      }
      getTree(number).then(res => {
        this.treeData = treeDataTranslate(res.data, 'number', 'name')
      })
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit'].includes(type)) {
        this.confirmLoading = true
        getObj(num).then(res => {
          const record = res.data
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'number', 'name', 'specifications', 'parentNumber', 'level', 'prope', 'matching', 'unit', 'remark']))
          })
        })
      } else {
        resetFields()
      }
    },
    // 结束保存用户信息
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          values.bomNumber = this.number
          if (this.type === 'add') {
            await addObj(values)
          } else if (this.type === 'edit') {
            await putObj(values)
          }
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('ok')
            this.visible = false
          }, 1500)
        }
      })
    },
    onChange (value) {
    },
    handleCancel () {
      this.visible = false
      setTimeout(() => {
        this.addable = false
        this.showable = false
        this.editable = false
      }, 200)
    }
  }
}
</script>

<style scoped>

</style>
